Appendix A : Commodore 64 BASIC V2 Summary Here is a short summary of the commands for the built in Commodore 64 BASIC V2. Note that this is not intended to be a tutorial, but just a quick reference to refresh long unused brain cells. In C64 BASIC V2 there are only few types of variables: • Float: This is the most frequently used type. Float-variables do not carry a type identifier. Range +/- [2.94E-39 ... 1.70E+38] (+/- [2^-128 ... 2^127]) Precision: 32 bit (approx. 9.6 decimal digits) • Integer (16 Bit): This is a rarely used type. Integer variables carry a '%' at the end of their name (eg. X%). Note that all computations are performed with float precision and conversion to Integer is only performed when the value is assigned to a variable. Thus Integer math is SLOWER (!) than Float math. • Boolean Results are expressed using Integers. FALSE is represented by 0 (0x0000) and TRUE by -1 (0xFFFF) or any other non-zero value. • Strings: String variables carry a '$' at the end of their name (eg. X$). Strings can be at most 255 characters long. Only the first TWO (!) characters of a variable name are significant. Program Flow Control: FOR...TO...STEP...NEXT - The only real loop construct in BASIC Syntax: FOR = TO [STEP ] NEXT [] Examples: FOR I=1 TO 5: PRINT I;: NEXT -> 1 2 3 4 5 FOR I=1 TO 5 STEP 2: PRINT I;: NEXT -> 1 3 5 FOR I=5 TO 1 STEP -2: PRINT I;: NEXT I -> 5 3 1 FOR I=3 TO 1: PRINT I;: NEXT -> 3 (!) IF...THEN - Conditional Program Execution Note: There is no such thing as ELSE or ENDIF. Syntax: IF THEN or IF GOTO or IF THEN Example: 100 IF A < B THEN MN = A: GOTO 120 110 MN = B 120 .... GOTO or GO TO - Unconditional Jump Syntax: GOTO or GO TO GOSUB - Unconditional Jump to Subroutine Note that it is not possible to use formal parameters to a subroutine. Everything must be done using global variables. Syntax: GOSUB Example: 10 PRINT "Main Program" 20 GOSUB 100 30 PRINT "Back To Main" 40 GOSUB 100 50 PRINT "Once again Main" 60 END 100 PRINT "This is the Subroutine" 110 RETURN RETURN - Return from Subroutine Syntax: RETURN Example: See GOSUB ON...GOTO or ON...GOSUB - Multiway branch Syntax: ON GOTO , ... or ON GOSUB , ... Example: ON X GOTO 100, 200, 300 This is equivalent to: IF X = 1 THEN GOTO 100 IF X = 2 THEN GOTO 200 IF X = 3 THEN GOTO 300 DEF FN - Define a BASIC Function/Subroutine Syntax: DEF FN () = Example: DEF FN SI(X) = SIN(X)/X FN SI(π/3) -> 0.816993343 Input/Output: GET - Read One Character from Standard Input without waiting Syntax: GET Example: 100 GET A$: IF A$ = "" THEN GOTO 100 -> Wait for any Key INPUT - Get Data from Standard Input (usually Keyboard) Syntax: INPUT [;] [, ...] Examples: INPUT "LOGIN:"; LG$ INPUT "Please Enter A, B and C"; A, B, C INPUT A PRINT - Write to Standard Output (usually Screen) Syntax: PRINT or: ? Examples: PRINT "Hello World" PRINT "Here", "are", "Tabs" -> Note the ',' PRINT "First Line"; -> Note the ';' PRINT "Still the same line" PRINT "Power"; 2*32 SPC - Advance the Cursor by a specific Number of Steps Syntax: SPC() Example: SPC(6) TAB - Advance the Cursor to a Specific Position Syntax: TAB() Example: TAB(6) POS - Current Cursor Position Syntax: POS() Files: LOAD - Load a Program from Disk or Tape Syntax: LOAD [, [, ]] Example: LOAD "SuperGame", 8, 1 -> Absolute Load from Disk #8 LOAD "*", 9 -> Load the first program from Disk #9 LOAD "", 1 -> Load the first program from Tape (#1) SAVE - Save a Program to Disk or Tape Syntax: SAVE [, [, ]] Example: SAVE "SuperGame", 8 -> Save to Disk #8 To overwrite an existing file on a disk, prefix the filename with '@' Example: SAVE "@SuperGame",8 -> Save to Disk #8, overwriting an old file VERIFY - Check if the Program in Memory and a Program on Disk or Tape are equal. Do not modify anything. Syntax: VERIFY [, [, ]] Example: VERIFY "SuperGame", 8 -> Check SuperGame from Disk #8 VERIFY "*", 9 -> Check the first program from Disk #9 VERIFY "", 1 -> Check the first program from Tape #1 OPEN - Open a File Syntax: OPEN , [, [, ]] The is an optional integer in the range 0-15 with the following meaning: 0..Used for LOAD, 1..Used for SAVE, 2-14..Freely usable for User File Access, 15..Command/Error Channel. uses the format: " [, [,]]" where is one of P (Program), S (Sequential), L (Relative) or U (User) and is one of R (Read), W (Write), A (Append) or the number of Bytes/Record for Relative Files. Example: OPEN 1, 4 -> Open a Output File to the Printer #4 OPEN 1, 8, 2, "My File,P,R" -> Open a Program for Reading OPEN 1, 8, 2, "My File,S,W" -> Open a Sequential File for Writing OPEN 1, 8, 2, "My File,L,"+CHR$(40) -> Open a Relative File with 40 Bytes/Record OPEN 1, 8, 15 -> Open the Disk Command/Error Channel CLOSE - Close a File Syntax: CLOSE Example: CLOSE 1 GET# - Read One Character from a File Syntax: GET# , Example: GET#1, A$ Note that there is no space between 'GET' and '#'. INPUT# - Get Data from a File Syntax: INPUT# , [, ...] Example: INPUT#1, EN$, ER$, TR$, SC$ Note that there is no space between 'INPUT' and '#'. PRINT# Syntax: PRINT# , Example: PRINT#1, "Power64" Note that there is no space between 'PRINT' and '#'. Note also that ?# is not PRINT# also they look the same in a listing. CMD - Redirect Standard Output (Input is not affected) and writes a Message to it Syntax: CMD [, ] Example: OPEN 1, 4 ; Open a File#1 on Printer#4 CMD 1 ; Make it the standard Output PRINT "Whatever Output you want" PRINT "More Output" PRINT#1 ; Undo CMD 1 CLOSE 1 ST - Device Status (Built-In Variable) ST = 0 .. Device Ok Bit 6: 1 .. End of File Bit 7: 1 .. Device Not Present READ - Read Static Data from DATA Statements in the Program Syntax: READ [, ...] Example: 10 RESTORE 20 READ X$ 30 PRINT X$; 40 S = 0 50 FOR I=1 TO 3 60 READ X 70 S = S + X 80 NEXT I 90 PRINT S 100 DATA "Power", 12, 34, 18 RESTORE - Set Pointer to Next DATA element to the first DATA statement in the program. Syntax: RESTORE Example: See READ DATA - Store Static Data Syntax: DATA [, ...] Example: See READ Math Functions: LET - Assignment Syntax: LET = Example LET A = 6.25 Note the LET keyword is not necessary. = is all that is needed. LET only slows things down -> Don't use it! DIM - Array Declaration Syntax: DIM ( [, ...]) Example: DIM A(7) -> An array of 8(!) elements indexed [0..7] DIM B$(4,5) -> An array of 30(!) strings Usage of Elements: A(3) = 17 : B$(2,3) = "Power64" +, -, *, / - Arithmetic Operators Example: 9 + 5 * (15 - 1) / 7 -> 19 <, <=, =, <>, >=, > - Comparison Operators Examples: 3 <> 6 -> -1 (TRUE) 3 > 4 -> 0 (FALSE) SIN - Sine (Argument in Radians) Syntax: SIN() Example: SIN(π/3) -> 0.866025404 COS - Cosine (Argument in Radians) Syntax: COS() Example: COS(π/3) -> 0.5 TAN - Tangent (Argument in Radians) Syntax: TAN() Example: TAN(π/3) -> 1.73205081 ATN - Arcus Tangent (Result in [-π/2 .. π/2]) Syntax: ATN() Example: ATN(1) -> 0.785398163 ( = π/4) EXP - Exponent (E^X where E = 2.71828183...) Syntax: EXP() Example: EXP(6.25) -> 518.012825 LOG - Natural Logarithm Syntax: LOG() Example: LOG(6.25) -> 1.83258146 SQR - Square Root Syntax: SQR() Example: SQR(6.25) -> 2.5 ABS - Absolute Value Syntax: ABS() Examples: ABS(-6.25) -> 6.25 ABS(0) -> 0 ABS(6.25) -> 6.25 SGN - Sign Syntax: SGN() Examples: SGN(-6.25) -> -1 SGN(0) -> 0 SGN(6.25) -> 1 INT - Integer (Truncate to greatest integer less or equal to Argument.) Syntax: INT() Examples: INT(-6.25) -> -7 (!) INT(-5) -> -5 INT(0) -> 0 INT(5) -> 5 INT(6.25) -> 6 RND - Random Number in [0.0 .. 1.0] Syntax: RND() If ( < 0) the Random number generator is initialized Examples: RND(-625) -> 3.85114436E-06 RND(0) -> 0.464844882 RND(0) -> 0.0156260729 Logic & Binary Operators: Recall the encoding of Boolean Values: FALSE <--> 0 (0x0000) and TRUE <--> -1 (0xFFFF) or any non-zero value AND - Logical & Binary AND Syntax: AND Example: A>5 AND X<=Y 12 AND 10 -> 8 (%1100 AND %1010 = %1000) OR - Logical & Binary OR Syntax: OR Example: A>5 OR X<=Y 12 OR 10 -> 14 (%1100 OR %1010 = %1110) NOT - Logical & Binary NOT Syntax: NOT Example: NOT A>5 NOT 2 -> -3 (NOT $0002 = $FFFD) Character & String Processing: + - Concatenate Strings Example: "Pow" + "er64" -> "Power64" <, <=, =, <>, >=, > - Comparison Operators Examples: "C64" < "Power64" -> -1 (TRUE) "Alpha" > "Omega" -> 0 (FALSE) LEN - Stringlength Syntax: LEN() Example LEN("Power64") -> 7 LEFT$ - Left part of a string Syntax: LEFT$(, ) Example: LEFT$("Power64", 5) -> "Power" RIGHT$ - Right part of a string Syntax: RIGHT$(, ) Example: RIGHT$("Power64", 5) -> "wer64" MID$ - Middle part of a string Syntax: MID$(, , ) Example: MID$("Power64 for Macintosh", 13, 3) -> "Mac" /* 123456789012345678901 */ STR$ - Convert a Number into a String Syntax: STR$() Example: STR$(6.25) -> " 6.25" STR$(-6.25) -> "-6.25" VAL - Convert a String to a Number Syntax: VAL() Examples: VAL("6.25") -> 6.25 VAL("6xx25") -> 6 VAL("x6x25") -> 0 ASC - ASCII code of the first character of a string Syntax: ASC() Examples: ASC("P") -> 80 ASC("Power64") -> 80 CHR$ - Character with a specific ASCII code Syntax: CHR$() Example: CHR$(80) -> "P" Memory Access: PEEK - Read Byte from Memory Syntax: PEEK() Example: PEEK(53280) -> Current Frame Color POKE - Write Byte to Memory Syntax: POKE , Example: POKE 53280, 7 -> Yellow Frame WAIT - Wait until a Byte in Memory has a specific value Syntax: WAIT , [, ] WAIT will halt the program until ((PEEK() EXOR ) AND ) != 0 If is not specified it is assumed to be 0. Example: WAIT 198, 255 -> Wait for a key in the key buffer. Interface to Assembler Programs: SYS - System - Call a Assembler Program Syntax: SYS [, ...] The number of parameters depends on the actual program called. USR - User Command Syntax: USR() Similar to SYS but the is fixed to $0310 and the first and only is already evaluated and stored in FloatAccu1 (FAC1) when the Assembler Program is called. Less flexible than SYS and thus rarely used. Program Execution: RUN - Start the BASIC Program Syntax: RUN [] If no is given, the program is started on its first line. Example: RUN STOP - Stops program execution Syntax: STOP STOP is similar to END, but prints the message BREAK IN when executed. END - End program execution Syntax: END CONT - Continue program execution Syntax: CONT When program execution has interrupted by STOP, END or the Run/Stop key, the command CONT can be used to resume execution. Miscellaneous: REM - Remark Syntax: REM Example: REM This line contains a comment LIST - Display the listing of the current BASIC program Syntax: LIST [ | - | - | -] Without argument, the entire program is listed. Examples: LIST LIST -40 LIST 100-200 NEW - Delete the current program and all variables from memory Syntax: NEW If the NEW command was accidentally issued, the deleted program can be recovered by using the NEW Magician described in Section 7.2. CLR - Delete all variables Syntax: CLR FRE - Free Memory Syntax: FRE() Example: FRE(0) -> -26627 (immediately after Power-on) Returns the number of Bytes free for BASIC programs as a signed 16 Bit integer. If the free memory exceeds 32KByte then a negative number (the actual number of free Bytes - 65536) will be returned. Thus -26627 should be read as 65536-26627 = 38909. π - Pi = 3.14159265 TI - Timer Ticks since Power-On (1 Tick = 1/60 Second) TI$ - Timer since Power-On in Hour/Minute/Second Format TI$ (but not TI) can be assigned a value! The accuracy of the Timer is very poor (>1% drift)